package com.szqhyxj.youxiangju.biz.card.credit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.biz.realNameAuth.bankcard.BankList;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.picker.OptionPicker;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.PickerUtil;
import com.szqhyxj.youxiangju.sys.components.view.popview.MsgChoicePopView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CustomAdapt;

/**
 * 信用卡信息
 */

public class CreaditDetailsActivity extends BaseActivity implements NavigationBar.NavigationBarListener,CreaditDetailsContarct.View, MsgChoicePopView.OnClickListener,CustomAdapt{

    private static final String CARDNO = "cardNo";

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.picture)
    ImageView picture;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.card_no)
    TextView cardNo;
    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.issuing_bank)
    TextView issuingBank;
    @BindView(R.id.phone_code)
    TextView phoneCode;
    @BindView(R.id.accountant_bill_date)
    TextView accountantBillDate;
    @BindView(R.id.due_date)
    TextView dueDate;
    @BindView(R.id.fixed_quota)
    EditText fixedQuota;
    @BindView(R.id.save)
    Button save;

    private CreaditDetailsPresenter presenter;
    private String cardNo1;

    public static void start(Activity activity,String cardNo){
        Intent intent = new Intent();
        intent.putExtra(CARDNO,cardNo);
        intent.setClass(activity,CreaditDetailsActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creadit_details);
        ButterKnife.bind(this);
        initViews();
        cardNo1 = getIntent().getStringExtra(CARDNO);
        presenter = new CreaditDetailsPresenter(this);
        presenter.init(cardNo1);
    }

    private void initViews(){
        navigationBar.setNavigationBarListener(this);
    }

    @OnClick({R.id.issuing_bank_line, R.id.accountant_bill_date, R.id.due_date,R.id.save})
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.accountant_bill_date:

                PickerUtil.onDayPicker(this, new OptionPicker.OnOptionPickListener(){
                    @Override
                    public void onOptionPicked(int index, String item){
                        accountantBillDate.setText(item);
                        presenter.setBillingDate(item.replace("日",""));
                    }
                });

                break;
            case R.id.due_date:
                PickerUtil.onDayPicker(this, new OptionPicker.OnOptionPickListener(){
                    @Override
                    public void onOptionPicked(int index, String item){
                        dueDate.setText(item);
                        presenter.setDueDate(item.replace("日",""));
                    }
                });
                break;
            case R.id.save:

                presenter.setBankCardNo(cardNo1);
                presenter.setFixedAmount(fixedQuota.getText().toString());
                presenter.save();
                break;
        }
    }



    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
        MsgChoicePopView popView = new MsgChoicePopView(this);
        popView.setOnClickListener(this);
        popView.setMsg("是否解绑当前信用卡");
        popView.show();
    }

    @Override
    public void finishThis(){
        finish();
    }

    @Override
    public void navRelieveCreaditOk(){
        RelieveCreaditOkActivity.start(this,"");
    }

    @Override
    public void showCreaditDetails(CardModel.Card card){
        LoadImageUtils.loadImageRotation(picture,card.getBankCardPic(),270
        );
        LoadImageUtils.loadImage(icon,card.getCardLogo());

        name.setText(card.getUsername());
        cardNo.setText(card.getBankCardNo());
        phoneCode.setText(card.getCardPhone());
        issuingBank.setText(card.getBtName());
        accountantBillDate.setText(card.getBillingDate());
        dueDate.setText(card.getDueDate());

        if(!TextUtils.isEmpty(card.getFixedAmount())){
            int leng = card.getFixedAmount().length();
            fixedQuota.setText(card.getFixedAmount().substring(0,leng-2));
        }


    }

    @Override
    public void showIssuser(List<BankList> bankLists){
        String[] bs = new  String[bankLists.size()];
        for(int i= 0;i<bankLists.size();i++){
            bs[i] = bankLists.get(i).getBtName();
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        presenter.onDestroy();
    }

    @Override
    public void onClickConfirm(View v){
        presenter.relieveBind(getIntent().getStringExtra(CARDNO));
    }

    @Override
    public boolean isBaseOnWidth(){
        return true;

    }

    @Override
    public float getSizeInDp(){
        return 360;
    }
}
